package com.demo.mircoauth.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mircoauth.entity.SysUser;
import com.demo.mircoauth.service.SysUserService;
import com.demo.mircoauth.mapper.SysUserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {

    @Resource
    PasswordEncoder encoder;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class,readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<SysUser> usernameQuery = new QueryWrapper<SysUser>().eq("username", username);
        //根据用户名选择用户
        SysUser sysUser = this.baseMapper.selectOne(usernameQuery);
        sysUser.setPassword(encoder.encode(sysUser.getPassword()));
        Assert.notNull(sysUser,"用户不存在");
        return sysUser;

    }






}
